package com.jf.hb.ui.interfaces;

import com.jf.hb.ui.JFHandler;

import javax.swing.*;
import java.awt.*;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-07
 * Time:1:13
 */
public class Menu extends Interface {
    private Box hbox;

    private Font font;

    private ImageIcon bk;
    private final ImageIcon[] bks;
    private int index = 0;

    private boolean live;
    public Menu() {
        //初始化背景动图
        bks = new ImageIcon[26];
        String bkPath;
        for (int i = 0; i < 26; i++) {
            bkPath = "/img/menu/bk (" + (i + 1) + ").png";
            bks[i] = new ImageIcon(Objects.requireNonNull(getClass().getResource(bkPath)));
        }
        bk = bks[index];

        hbox = Box.createHorizontalBox();
        font = new Font("华文行楷", Font.PLAIN, 48);

        initButtons();
        this.add(hbox);

        live = true;
    }
    private void initButtons() {
        Box buttonBox = Box.createVerticalBox();

        JButton giftButton = new JButton("新礼物", new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/buttons/button1.png"))));
        giftButton.setHorizontalTextPosition(SwingConstants.CENTER);
        giftButton.setFont(font);
        giftButton.setContentAreaFilled(false);
        giftButton.setBorderPainted(false);
        giftButton.setBorder(null);
        giftButton.addActionListener(e->{
            JFHandler.menuChoice = JFHandler.MENU_CHOICE.NEW_GIFT;
            JFHandler.switchSignal = JFHandler.IF.MENU;
            live = false;
        });

        JButton numButton = new JButton("幸运数字", new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/buttons/button2.png"))));
        numButton.setHorizontalTextPosition(SwingConstants.CENTER);
        numButton.setFont(font);
        numButton.setContentAreaFilled(false);
        numButton.setBorderPainted(false);
        numButton.setBorder(null);
        numButton.addActionListener(e -> {
            JFHandler.menuChoice = JFHandler.MENU_CHOICE.LUCKY_NUMBER;
            JFHandler.switchSignal = JFHandler.IF.MENU;
            live = false;
        });

        JButton wishButton = new JButton("愿望清单", new ImageIcon(Objects.requireNonNull(getClass().getResource("/img/buttons/button3.png"))));
        wishButton.setHorizontalTextPosition(SwingConstants.CENTER);
        wishButton.setFont(font);
        wishButton.setContentAreaFilled(false);
        wishButton.setBorderPainted(false);
        wishButton.setBorder(null);
        wishButton.addActionListener(e -> {
            JFHandler.menuChoice = JFHandler.MENU_CHOICE.WISHLIST;
            JFHandler.switchSignal = JFHandler.IF.MENU;
            live = false;
        });

        hbox.add(Box.createHorizontalStrut(768));//  3/5=768  3/4=960

        buttonBox.add(Box.createVerticalStrut(70));
        buttonBox.add(giftButton);
        buttonBox.add(Box.createVerticalStrut(50));
        buttonBox.add(numButton);
        buttonBox.add(Box.createVerticalStrut(50));
        buttonBox.add(wishButton);
        hbox.add(buttonBox);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bk.getImage(), 0, 0, getWidth(), getHeight(), null);
    }

    @Override
    public void run() {
        while (live) {
            index++;
            if (index >= 26) {
                index = 0;
            }
            bk = bks[index];
            repaint();
            try {
                Thread.sleep(150);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        live = true;
    }
}
